<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>商品详情</title>
<link rel="stylesheet" type="text/css" href="../static/css/style.css">
<link rel="stylesheet" type="text/css" href="../static/css/shoujisc.css">
<script type="text/javascript" src="../static/js/jquery-2.0.0.min.js"></script>
<script type="text/javascript" src="../static/js/myinit.js"></script>
<script type="text/javascript" src="../static/js/jquery.mobile-1.4.5.min.js"></script>
<script type="text/javascript" src="../static/js/xujianbanner.js"></script>
<script type="text/javascript" src="../static/js/cook_shop_cart.js"></script>

<script type="text/javascript" src="../static/js/pinyin.js"></script>



<style type="text/css">
	.mypageback{
		float:left;
	}
	
	
	
	.myctlgroup{
		margin-bottom:5px;
		margin-top:5px;
	
		
	}
	
	.myradio{
		padding:4px 8px;
		margin-left:4px;
		display:inline-block;
		text-align:center;
		border: 1px solid #dfdfdf;
	
		
	}
	.myradiochecked{
		border:1px solid #df3133;
		background-image:url('/static/img/gou.png');
		background-repeat:no-repeat;
		background-position:right bottom;
		background-size:10px 10px;
	}
	
	.mygroup{
		border:1px solid #dcdcdc;
		display:inline-block;
		height:30px;
	}
	
	.mygroupitem{
		display:inline-block;
		
		background-position:center;
		background-repeat:no-repeat;
		background-size:20px 20px;
		height:30px;
		min-width:30px;
	
	}
	
	 
	

</style>
</head>

<body>

  
    <div style="width:100%;overflow-x:hidden;border-bottom:1px solid #e6e6e6">
	<div id="banner" style="position:relative;overflow:hidden;min-height:50px;">   </div></div>
	
	
    <div class="spxq-info1">
		<div id="goodname_h2" style="font-size:16px"></div>
    	<ul class="spxq-ul1" style="font-size:16px;margin-top:8px">
        	<li>
            	 
                <div>
                	<p class="spxq-p1 f-l" style="font-size:14px;">价格：<span>￥<span id="price"></span></span></p>
                	<p class="spxq-p2 f-r" style="font-size:14px;">销量：<span id="already_sell"></span>件</p>
                    <div style="clear:both;"></div>
                </div>
            </li>
        </ul>
		
		
		 
		<div id="guigelist_new" style="font-size:14px;margin-top:10px;margin-bottom:10px"></div>
		<div style="line-height: 30px;font-size:14px;" id="shuliang" >数量：
			<div style="vertical-align: middle;" class="mygroup">
				<div class="mygroupitem" style="background-image: url(&quot;/static/img/jianhao.png&quot;); width: 30px; height: 30px;border-right:1px solid #dcdcdc;" id="minusOne"></div>
				<div class="mygroupitem" style="  height: 30px;">
					<input style="float: left; line-height: 30px; height: 30px; border: medium none; text-align: center; background-color: transparent; width: 60px;font-size:16px" id="thiscount" value="1" type="tel">
				</div>
				<div class="mygroupitem" id="addOne" style="background-image: url(&quot;/static/img/jiahao.png&quot;); width: 30px; height: 30px;border-left:1px solid #dcdcdc;"></div>
			</div>
            <div style="margin-left:70%;margin-top:-31.5px"><img src="/static/img/star.png"width="30px" height="30px" />
            
            </div>
            <div id="shifouchoucang"></div>
            
		</div>
		
        <div class="spxq-if1">
        	<p>活动公告：活动期间赠送商品总额5%的资金</p>
        </div>
    </div>
    <div class="spxq-info2" style="margin-bottom: 50px;">
    	<ul class="spxq-ul2">
        	<li class="current" style="width:50%"><a href="JavaScript:;">商品详情</a></li>
        	<li style="width:50%" id="showpingjia"><a href="JavaScript:;" >客户评价</a></li>
            <div style="clear:both;"></div>
        </ul>
        <div class="spxq-box">
        	<div class="spxq-k spxq-k1" id="desc" style="text-align: left;">
            	 
            </div>
            <div class="spxq-k spxq-k2" style="display:none;" id="pingjia_list"></div>
        </div>
    </div>
    
    <div class="spxq-info3" style="position:fixed;height:48px">
    	<img style="height: 40px; float: left; margin-left: 2%; margin-top: 5px; width: auto;" src="/static/img/sjsc-16.png" id="talktokefu">
    	<button class="if3-btnn if3-btn1 f-l" style="width:30%;height:38px;margin-left:6%" id="addtoCartAndToShoppinglist">点此购买</button>
    	<button class="if3-btnn if3-btn2 f-l" style="margin-left:3%;width:29%;height:38px" id="addtoCart">加入购物车</button>
		
		 
        <a id="tocart" class="if3-aa f-l" style="width:10%;margin-top:5px;margin-left:4%"><img src="/static/img/sjsc-17-2.png" /></a>
		<div style=" background-color: rgb(210, 36, 36); border-radius: 50px; width: 12px; height: 12px; text-align: center; line-height: 12px; font-size:8px;font-weight:lighter;color: white; right: 14px;top:7px;position:absolute;text-shadow:none" id="cart_total">0</div>
    </div>
	
	<script type="text/javascript">
		$.ajaxSettings.async = false;
		$.ajaxSettings.cache=false;

		$('.mypageback').click(function(){
			window.history.go(-1);
			
		});
		 
		var name;
		var price;
		var img;
		var id;
		 
		var openid =getUrldata('OPENID');
	
		var goodid=getUrldata('GOODID');
		
		addshangpinhistory(goodid);
		id=goodid;
		
		$(document).ready(function(){
			initThisGood();
			refreshCartTotal();
			getAllGuige();
			PDshoucang();
		});

		
		function PDshoucang(){
			var url = "/webserver/api/v1/userfavorite/?format=json&openid="+openid;
			var len = 0;
			var pdsl= 0;
			var ww=$(document).innerWidth();
			$.getJSON(url,function(rs){
				console.log(rs);
				var objects = rs.objects;
				
				//----徐剑添加开始
				var iscontain=0;
				$.each(objects,function(index,item){
					var goods=item.goods;
					if(goods.id==goodid){
						iscontain++;
						
					}
				});
				if(iscontain>0){
					//已经添加过了
					$('#shifouchoucang').append('<div style="font-size:12px;color:#999;margin-left:80%;margin-top:-28px" id="yijingshouchangle">已收藏</div>');
					$('#yijingshouchangle').click(function(e) {
						//取消收藏 取消成功记得刷新页面
						
						
					});
					
				}else {
					//没有添加
					$('#shifouchoucang').append('<div style="font-size:12px;color:#999;margin-left:80%;margin-top:-28px"  id="shoucang">添加收藏</div>');
					$('#shoucang').click(function(e) {
						//添加收藏 添加收藏成功记得刷新页面
						var aa;
						var url = "/webserver/api/v1/userfavorite/";
						$.post(url,{"openid":openid,"goodid":'/webserver/api1/good/?format=json&goods='+goodid},function(rs){
							aa=rs;
						},'json');
						var aa={};
						aa.openid= openid;
						aa.goods='/webserver/api1/good/'+goodid+'/';
						dopost(url,aa,function(rs){
							alert('收藏成功！');
							document.location.href="xiangqing.html?OPENID="+openid+"&GOODID="+goodid;
						});	
					});
				}
				
				//徐剑添加结束
				if(objects.length!=0){
					for(len=0;len<object.length;len++){
						if(item.goods.id==goodid){
							$('#shifouchoucang').append('<div style="font-size:12px;color:#999;margin-left:80%;margin-top:-28px">已收藏</div>');
							pdsl=1;
						}
					}
					if(pdsl!=1){
						$('#shifouchoucang').append('<div style="font-size:12px;color:#999;margin-left:80%;margin-top:-28px"  id="shoucang">添加收藏</div>');
					}
				}
			},'json');
			$.getJSON(url,function(rs){
				var objects = rs.objects;
			});	
		}
		
		function initThisGood(){
			var url="/webserver/api/v1/good/?format=json&id="+goodid;
			$.getJSON(url,function(rs){
			 
				var gd=rs.objects[0];
				$('#goodname').html(gd.name);
				$('#goodname_h2').html(gd.name);
				name=gd.name;
				price=gd.price;
				img=gd.image_thumb;
				$('#price').html(price);
				$('#already_sell').html(gd.sales);
				
				
				$('#desc').html(gd.desc);
				$('#desc img').css('display','block');
				var descimgs=[];
				$('#desc img').each(function(index, element) {
					
					descimgs.push("http:\/\/www.tiantianhw.cn"+$(this).attr('src').replace("//","\/"));
					$(this).click(function(e) {
						WeixinJSBridge.invoke('imagePreview',{
							'current' : descimgs[index],
							'urls' : descimgs
						});
						
					});
				});
				
				
				for(var i=1;i<6;i++){
					var imgsrc=gd['image'+i];
					if(imgsrc!=''&&imgsrc!=null&&imgsrc!='null'){
						var len=$('#banner img').length;
						$('#banner').append('<img src="'+imgsrc+'" style="position:absolute;display:block;width:100%">');
						 
						
					}
					
				}
				var len=$('#banner img').length;
				if(len>1){
					$('#banner').append('<img src="'+gd['image1']+'" style="position:absolute;display:block;width:100%">');
					
				
				}
				
				
				var srcList = [];
				$('#banner img').each(function(index, element) {
					
					srcList.push("http:\/\/www.tiantianhw.cn"+$(this).attr('src').replace("//","\/"));
					$(this).click(function(e) {
						WeixinJSBridge.invoke('imagePreview',{
							'current' : srcList[index],
							'urls' : srcList
						});
						
					});
				});
				 
				
				initBanner(151/153);
			
			
             
			
			});
		}
		
		$('#addtoCart').click(function(){
			addACart(goodid,name,price,img);
			refreshCartTotal();
			 
		});
		function addACart(idd,na,pr,im){
			//id,goodid, name, count, price,getguigehtm();
			var count=parseInt($('#thiscount').val());
			if(checkedisOkToadd()){
				cartHelper.add(getid(),idd,na,count,pr,im,getguigehtm());
				refreshCartTotal();
				return true;
			}else{
			
				return false;
			}
			
		
		}
		$('#tocart').click(function(){
			document.location.href="shoppinglist.html?OPENID="+openid;
		});
		
		function getAllGuige(){
			var url = "/webserver/api/v1/format/?format=json&goods="+goodid;
			$.getJSON(url,function(rs){
				var objects=rs.objects;
				if(objects.length!=0){
					$.each(objects,function(index,item){
						var ggname = item.catagory;
						var ggval = item.formatname;
						var ggid = pinyin.getFullChars(ggname);
						if($('#'+ggid).length==0){
							 
							$('#guigelist_new').append('<div class="myctlgroup" id="'+ggid+'"><span>'+ggname+'</span>：<div class="myradio">'+ggval+'</div></div>');
						}else{
							$('#'+ggid).append('<div class="myradio">'+ggval+'</div>');
					
						
						}
						
						
						
						
						
						
						
					
					});
					initRadioGroup();
				}
				
			});
		
		}
		function refreshCartTotal(){
			var num=cartHelper.getCart().count;
			if(num>0){
				$('#cart_total').html(num);
				$('#cart_total').show();
			}else{
				$('#cart_total').hide();
			}
		}
		$('#addtoCartAndToShoppinglist').click(function(){
			if(addACart(goodid,name,price,img)){
				document.location.href="shoppinglist.html?OPENID="+openid;
			}
			 
			
		});
		function getguigehtm(){
			var htm="";
			$('#guigelist_new .myctlgroup').each(function(index,item){
				var name = $(this).find('span').html();
				var aa=$(this).find('.myradiochecked').html();
				if(htm!=''){
					htm+=" ";
				}
				htm+=name+aa;
			});
			
			return htm;
		}
		
		function getid(){
			var htm="";
			$('#guigelist_new .myctlgroup').each(function(index,item){
				var aa=$(this).find('.myradiochecked').html();
				var name = $(this).find('span').html();
				if(htm!=''){
					htm+="_";
				}
				htm+=pinyin.getFullChars(name+aa);
			});
		
		
			return goodid+"_"+htm;
		}
		
		function checkedisOkToadd(){
			var isok=true;
			$('#guigelist_new .myctlgroup').each(function(index,item){
				var name = $(this).find('span').html();
				var aa=$(this).find('.myradiochecked').length;
				if(aa==0&&isok){
					
					alert('请选择'+name);
					isok=false;
					
				}
			
			});
			return isok;
		}
		
		
		
		function initRadioGroup(){
			$('.myctlgroup').each(function(index,item){
				var aa=$(this);
				$(this).find('.myradio').click(function(){
					if($(this).hasClass('myradiochecked')){
						$(this).removeClass('myradiochecked');
						 
					}else{
						aa.find('.myradiochecked').removeClass('myradiochecked');
						$(this).addClass('myradiochecked');
						 
					}
					
					
				
				});
			
			});
		}
		
		
		
		$('#addOne').click(function(){
			var count=$('#thiscount').val();
			count=parseInt(count)+1;
			$('#thiscount').val(count);
			$('#minusOne').css('background-image','url("/static/img/jianhao_black.png")');
		
		});
		$('#minusOne').click(function(){
			var count=parseInt($('#thiscount').val());
			if(count>1){
				$('#thiscount').val(--count);
				if(count==1){
					$('#minusOne').css('background-image','url("/static/img/jianhao.png")');
				}
			}
			
		
		});
		
		$('#showpingjia').click(function(e) {
			var htm=$('#pingjia_list').html();
			if(htm==''){
				var url = "/webserver/api/v1/custassess/?format=json&goods="+goodid;
				$.getJSON(url,function(rs){
					console.log(rs);
					var objects=rs.objects;
					$.each(objects,function(index,item){
						var time=item.modified.replace('T',' ');
						var openid=item.wx_id;
						var info=getWeiXin(openid);
						
						
						$('#pingjia_list').append('<div class="k2-if"><p class="k2-top">评论时间：'+time+'</p><dl class="k2-ft"><dt><a href="#"><img src="'+info.headimgurl+'" /></a></dt><dd><p class="k2-p1">评价：<span>'+item.assesscont+'</span></p><p class="k2-p2">'+info.nickname+'</p></dd><div style="clear:both;"></div></dl></div>');
					});
				},'json');
            
        		
		
			}
		});
		
		
		function getWeiXin(openid){
			var aa;
			$.post('/weixin/getCustinfo/',{"openid":openid},function(rs){
				aa=rs;
			},'json');
			return aa;
		}
		$('#talktokefu').click(function(){
			document.location.href="chatwithkefu.html?OPENID="+openid;
		});

	</script>

</body>
</html>
